Imports System.IO

Public Class SaveTextToFile


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim FileDB As New SaveFileDialog()

        FileDB.Filter = "All files | *.* | Word files |*.doc | Text files | *.txt"

        FileDB.FilterIndex = 3
        FileDB.InitialDirectory = "C:\Temp"
        FileDB.AddExtension = True
        FileDB.DefaultExt = "txt"

        If (FileDB.ShowDialog() = DialogResult.OK) Then
            Dim FS As FileStream

            Try
                FS = New FileStream(FileDB.FileName, FileMode.Create)
            Catch
                MsgBox("Loi File " & FileDB.FileName)
            End Try

            Dim I As Integer

            Try
                For I = 0 To TextBox1.Text.Length - 1
                    FS.WriteByte(Asc(TextBox1.Text.Chars(I)))
                Next
            Catch
                MsgBox("Loi ghi file")
            End Try

            FS.Close()
            MsgBox("Noi dung da luu " & FileDB.FileName)
        Else
            MsgBox("Cancel")
        End If

    End Sub


End Class
